/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import healthcare.coursework.model.MedicalHistory;
import healthcare.coursework.model.Patient;
import healthcare.coursework.service.MedicalHistoryService;
import healthcare.coursework.service.PatientService;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Window;

/**
 *
 * @author Administrator
 */
public class ViewMedicalHistories extends GenericAutowireComposer {

    //Componennt
    private Listbox medicalHistoriesDataListView;
    //Service
    private MedicalHistoryService medicalService = util.Utility.getUtility().getMedicalHistoryService();
    private PatientService patientService = util.Utility.getUtility().getPatientService();
    private Paging patientPaging;
    private Patient patient;
    private List<MedicalHistory> medicalHistories;
    private Label patientName;
    private   Long patientId;

    public ViewMedicalHistories() {
    }

    private void getParameter() {
        try {
          patientId = Long.valueOf(Executions.getCurrent().getParameter("patientId"));
           
        } catch (Exception e) {
            System.out.println("Get parameter is errors");
        }

    }

    private void setInfo() {
         medicalHistories = medicalService.listMedicalHistories(patientId);
            patient = patientService.findPatient(patientId);
        patientName.setValue("Name of patient: " + patient.getFullName());
        medicalHistoriesDataListView.setModel(new ListModelList(medicalHistories));
    }

    public void onLoadList(Event event) {
        System.out.println("Tesst");
        getParameter();
        setInfo();
    }

    public void onRemoveMedicalHistory(Event event) {

        try {
            int status = Messagebox.show("Are you sure to remove the medical history?", "Question", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION, new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    if (((Integer) event.getData()).intValue() == Messagebox.YES) {
                        Iterator it = medicalHistoriesDataListView.getSelectedItems().iterator();
                        System.out.println(medicalHistoriesDataListView.getSelectedItems().size());
                        while (it.hasNext()) {
                            Listitem li = (Listitem) it.next();
                            MedicalHistory m = (MedicalHistory) li.getValue();
                            medicalService.removeMedicalHistory(m.getMedicalHistoryId());
                            System.out.println("The patient medical history with Id "
                                    + m.getMedicalHistoryId() + " has been removed");
                        }
                        
                        setInfo();
                        return;
                    } else {
                        System.out.println("Messagebox.No selected!");
                        return;
                    }
                }
            });



        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void onViewMedicalHistoryInfo(Event event) {
        try {
            Map params = new HashMap();
            params.put("medicalHistory", (MedicalHistory) medicalHistoriesDataListView.getSelectedItem().getValue());
            System.out.println("Edit: " + ((MedicalHistory) params.get("medicalHistory")).getMedicalHistoryId());
            Window win = (Window) Executions.createComponents(
                    "/WEB-INF/zul/pages/doctor/ViewMedicalHistory.zul", null, params);

            win.setMaximizable(true);
            win.setClosable(true);
            win.doModal();

        } catch (NullPointerException e) {
            try {
                Messagebox.show("Please choose one medical history to display information?", "Information", Messagebox.OK, Messagebox.INFORMATION);
            } catch (InterruptedException ex) {
                Logger.getLogger(ViewMedicalHistories.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (Exception e) {

            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    public void onAddMedicalHistory(Event e) {
        try {
            Map params = new HashMap();
            params.put("patient", patient);
            Window win = (Window) execution.createComponents(
                    "/WEB-INF/zul/pages/doctor/AddMedicalHistory.zul", null, params);
            win.setMaximizable(true);
            win.setClosable(true);
            win.doModal();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}
